package achi.community.irla.database;

import achi.community.irla.entities.Achievements;
import achi.community.irla.entities.Achievements_completed;
import achi.community.irla.entities.Users;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

/**
 * ModelBean:
 * Contains logged in user
 */
@ManagedBean
@SessionScoped
public class ModelBean implements Serializable {

    @EJB
    DatabaseImpl db;
    private Users user;
    
    @ManagedProperty(value = "#{achievementsHandler}")
    AchievementsHandler achiHandler;

    public AchievementsHandler getAchiHandler() {
        return achiHandler;
    }

    public void setAchiHandler(AchievementsHandler achiHandler) {
        this.achiHandler = achiHandler;
    }

    public Users getUser() {
        return user;
    }

    public void setUser(Users user) {
        this.user = user;
        init();
        System.out.println("user set!");
    }
    
    public void init(){
        HashMap<String,Achievements> targetMap = db.getUserTargets(user.getId());
        HashMap<String,Achievements_completed> completedMap = db.getUserCompleted(user.getId());
        achiHandler.setTargetMap(targetMap);
        achiHandler.setCompletedMap(completedMap);
    }

    public boolean isLoggedin() {
        if (user == null) {
            user = new Users();
        }

        String u = user.getFirstName();
        return u != null;
    }

    public String logout() {
        user = null;
        return "login";
    }

    public List getAllUsers() {
        return db.getAllUsers();
    }

    public List getAllMessages() {
        return db.getAllMessages();
    }

    public List getAllFriends() {
        return db.getAllFriends();
    }

    public List getAllAchievements() {
        return db.getAllAchievements();
    }

    public List getAllAchievements_comments() {
        return db.getAllAchievements_comments();
    }

    public List getAllAchievements_completed() {
        return db.getAllAchievements_completed();
    }

    public List getAllAchievements_suggested() {
        return db.getAllAchievements_suggested();
    }

    public List getAllAchievements_target() {
        return db.getAllAchievements_target();
    }
    
    public Achievements getAchievement(String name) {
        List<Achievements> list = getAllAchievements();
        for(Achievements achi: list){
            String sname = achi.getTitle_sv();
            if(sname.equals(name))
                return achi;
        }
        return null;
    }
    
    public Achievements_completed getAchiCompleted(String id){
        return db.getAchiCompleted(id);
    }
    
    public void updateTarget(String dropPlace, Achievements achi){
        db.updateTarget(getUser().getId(), dropPlace, achi);
    }
    
    public void updateCompleted(Achievements_completed achi){
        db.updateCompleted(achi);
    }
}
